package com.formen.ndk;

import java.nio.charset.*;
import java.text.*;
import java.util.*;

import android.app.*;
import android.nfc.*;
import android.os.*;
import android.widget.*;

public class NFCSend extends Activity {

	//어댑터 객체 선언 : 
    private NfcAdapter mAdapter;
    private NdefMessage mMessage;

    private TextView mText;
    
    static long now = System.currentTimeMillis();
    
    static Date date = new Date(now);
    
    static SimpleDateFormat CurDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    static String strNow = CurDateFormat.format(date);
    
    //public static String sourceMsg = "lee@이장협@01028368406@서울@891008";
    public static String sourceMsg = MainActivity.user_id+"@"
    								+MainActivity.user_name+"@"
    								+MainActivity.user_number+"@"
    								+MainActivity.user_location+"@"
    								+MainActivity.user_birth+"@"
    								+strNow+"@"
    								+"GPS";										// 정한이 니 할부분
    
    
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        mAdapter = NfcAdapter.getDefaultAdapter(this);	//어댑터 객체 참조

        setContentView(R.layout.nfc_send);

        mText = (TextView) findViewById(R.id.nfc_send_tv1);
        if (mAdapter != null) {
            mText.setText("푸쉬할 메시지 : " + sourceMsg);
        } else {
            mText.setText("먼저 NFC를 활성화하세요.");
        }
        
        /*
        String filePath= Environment.getExternalStorageDirectory()
                + "/E-Profile/ugc.jpg";
		Bitmap selectedImage =  BitmapFactory.decodeFile(filePath);
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
		byte[] byteArray = stream.toByteArray();
		String strBase64=Base64.encodeToString(byteArray, 0);
        
		sourceMsg+="@"+strBase64;
		*/
        mMessage = new NdefMessage(
                new NdefRecord[] { createTextRecord(sourceMsg, Locale.ENGLISH, true),NdefRecord.createApplicationRecord("com.formen.ndk")}
        		);

    }

    public void onResume() {
        super.onResume();
        //if (mAdapter != null) mAdapter.enableForegroundNdefPush(this, mMessage);
        if (mAdapter != null) mAdapter.setNdefPushMessage(mMessage, this);
    }

    public void onPause() {
        super.onPause();
        //if (mAdapter != null) mAdapter.disableForegroundNdefPush(this);
        if (mAdapter != null) mAdapter.setNdefPushMessage(null, this);
    }

    public static NdefRecord createTextRecord(String text, Locale locale, boolean encodeInUtf8) {
        byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));

        Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
        byte[] textBytes = text.getBytes(utfEncoding);

        int utfBit = encodeInUtf8 ? 0 : (1 << 7);
        char status = (char) (utfBit + langBytes.length);

        byte[] data = new byte[1 + langBytes.length + textBytes.length];
        data[0] = (byte) status;
        System.arraycopy(langBytes, 0, data, 1, langBytes.length);
        System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
        
        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
    }

}